﻿Imports System.Data
Imports System.Data.SqlClient
Imports FreeTextBoxControls

Partial Class ntintucct
    Inherits System.Web.UI.Page


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub



    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim blnAddNew As Boolean
    Protected WithEvents ltest As System.Web.UI.WebControls.Label
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim intID As Int16

        If Not Page.IsPostBack Then
            hideText.Text = -1
            blnAddNew = True
            Button1.Text = "Thêm mới"

            Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
            Dim sSql As String = "select idloaitt,tenloaitt from loaitt"
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(sSql, myConn)
            Dim myDataSet As New DataSet
            myAdapter.Fill(myDataSet, sSql)
            loaitt.DataSource = myDataSet
            loaitt.DataBind()
            sSql = "select idnn,ngonngu from ngonngu"
            myAdapter = New SqlDataAdapter(sSql, myConn)
            Dim myDataSet2 As New DataSet
            myAdapter.Fill(myDataSet2, sSql)
            idnn.DataSource = myDataSet2
            idnn.DataBind()
            myConn.Close()
            If IsNumeric(Request.QueryString("id")) Then
                intID = Request.QueryString("id")
                BindGrid(intID)
                If Request.QueryString("new") <> 1 Then
                    blnAddNew = False
                    Button1.Text = "Cập nhật"
                End If
            Else
                TT.Checked = True
                If IsNumeric(Request.QueryString("c")) Then
                    loaitt.SelectedValue = Request.QueryString("c")
                End If

            End If
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim sSql As String
        If hideText.Text <= 0 Then
            sSql = "insert into Tintuc (ngay,tieude,tukhoa,idloaitt,idnn,gioithieu,hinhnho,TT,uutien,TB,idvn,canhle,tinnong,tkchude,phude,hinhqc,tieude_rewrite) " & _
                    "values(GetDate(),@tieude,@tukhoa,@idloaitt,@idnn,@gioithieu,@hinhnho,@TT,@uutien,@TB,@idvn,@canhle,@tinnong,@tkchude,@phude,@hinhqc, @tieude_rewrite)"
            If uutien.Text = "" Then
                sSql = "insert into Tintuc (ngay,tieude,tukhoa,idloaitt,idnn,gioithieu,hinhnho,TT,TB,idvn,canhle,tinnong,tkchude,phude,hinhqc,tieude_rewrite) " & _
                    "values(GetDate(),@tieude,@tukhoa,@idloaitt,@idnn,@gioithieu,@hinhnho,@TT,@TB,@idvn,@canhle,@tinnong,@tkchude,@phude,@hinhqc,@tieude_rewrite)"
            End If
            If canhle.Text = "" Then
                sSql = "insert into Tintuc (ngay,tieude,tukhoa,idloaitt,idnn,gioithieu,hinhnho,TT,uutien,TB,idvn,tinnong,tkchude,phude,hinhqc,tieude_rewrite) " & _
                    "values(GetDate(),@tieude,@tukhoa,@idloaitt,@idnn,@gioithieu,@hinhnho,@TT,@uutien,@TB,@idvn,@tinnong,@tkchude,@phude,@hinhqc,@tieude_rewrite)"
            End If

            If uutien.Text = "" And canhle.Text = "" Then
                sSql = "insert into Tintuc (ngay,tieude,tukhoa,idloaitt,idnn,gioithieu,hinhnho,TT,TB,idvn,tinnong,tkchude,phude,hinhqc,tieude_rewrite) " & _
                    "values(GetDate(),@tieude,@tukhoa,@idloaitt,@idnn,@gioithieu,@hinhnho,@TT,@TB,@idvn,@tinnong,@tkchude,@phude,@hinhqc,@tieude_rewrite)"
            End If
            blnAddNew = True
        Else
            sSql = "update Tintuc set" & _
            " tieude=@tieude,tukhoa=@tukhoa,idnn=@idnn,gioithieu=@gioithieu,idvn=@idvn,TB=@TB" & _
            " ,hinhnho=@hinhnho,TT=@TT,uutien=@uutien,idloaitt=@idloaitt,canhle=@canhle,tinnong=@tinnong,tkchude=@tkchude,phude=@phude,hinhqc=@hinhqc, tieude_rewrite=@tieude_rewrite" & _
            " where idtintuc=@idtintuc"
            blnAddNew = False
        End If
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If Not blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idtintuc", hideText.Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@tieude", tieude.Text))
        MyCommand.Parameters.Add(New SqlParameter("@phude", phude.Text))
        MyCommand.Parameters.Add(New SqlParameter("@hinhqc", hinhqc.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tukhoa", tukhoa.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tkchude", tkchude.Text))
        MyCommand.Parameters.Add(New SqlParameter("@TT", TT.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@tinnong", tinnong.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@idnn", idnn.SelectedValue))
        If uutien.Text <> "" Then
            'MyCommand.Parameters.Add(New SqlParameter("@uutien", 0))
            ' Else
            MyCommand.Parameters.Add(New SqlParameter("@uutien", uutien.Text))
        End If
        If canhle.Text <> "" Then
            '   MyCommand.Parameters.Add(New SqlParameter("@canhle", 0))
            'Else
            MyCommand.Parameters.Add(New SqlParameter("@canhle", canhle.Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@hinhnho", hinhnho.Text))
        MyCommand.Parameters.Add(New SqlParameter("@idvn", idvn.Text))
        MyCommand.Parameters.Add(New SqlParameter("@gioithieu", gioithieu.Text))
        MyCommand.Parameters.Add(New SqlParameter("@idloaitt", loaitt.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@TB", thongbao.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@tieude_rewrite", tieude_rewrite.Text))
        MyCommand.ExecuteNonQuery()
        If blnAddNew Then
            sSql = "select max(idtintuc) from tintuc"
            MyCommand.CommandText = sSql
            hideText.Text = MyCommand.ExecuteScalar
        End If
        sSql = "update tintuc set noidung =N'" & Replace(noidung.Text, "'", "''") & "' where idtintuc =" & hideText.Text
        MyCommand.CommandText = sSql
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        Response.Redirect("ntintuc.aspx?l=" & Request.QueryString("l"))


    End Sub
    Private Sub BindGrid(ByVal vId As Int16)
        On Error Resume Next
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select top 1 tieude,phude, hinhqc, tukhoa,idtintuc, idloaitt,idnn,TT,uutien,gioithieu,noidung,hinhnho,idvn,canhle,tinnong,TB,tkchude,tieude_rewrite from tintuc where idtintuc = " & vId
        myConn.Open()
        Dim myComm As New SqlCommand(sSql, myConn)
        Dim myDr As SqlDataReader
        myDr = myComm.ExecuteReader
        If myDr.Read Then
            tieude.Text = myDr("tieude")
            phude.Text = myDr("phude")
            hinhqc.Text = myDr("hinhqc")
            tukhoa.Text = myDr("tukhoa")
            canhle.Text = myDr("canhle")
            uutien.Text = myDr("uutien")
            tkchude.Text = myDr("tkchude")
            gioithieu.Text = myDr("gioithieu")
            noidung.Text = myDr("noidung")
            idnn.SelectedValue = myDr("idnn")
            TT.Checked = myDr("TT")
            tinnong.Checked = myDr("tinnong")
            thongbao.Checked = myDr("TB")
            loaitt.SelectedValue = myDr("idloaitt")
            hinhnho.Text = myDr("hinhnho")
            idvn.Text = myDr("idvn")
            If Request.QueryString("new") <> 1 Then
                hideText.Text = myDr("idtintuc")
            End If
            tieude_rewrite.Text = myDr("tieude_rewrite")
        End If
        myConn.Close()
    End Sub
End Class
